<?php
/**
 * @title 236. 二叉树的最近公共祖先
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */

class Solution {
    /**
     * @param TreeNode $root
     * @param TreeNode $p
     * @param TreeNode $q
     * @return TreeNode
     */
    function lowestCommonAncestor($root, $p, $q) {
        /**
         * @since 2023-12-25 父节点数组，子节点值=>父节点TreeNode
         */
        $this->parentArray = [];
        $this->ParentRelation($root, null);
        unset($this->parentArray[$root->val]);

        /**
         * @since 2023-12-25 $p到根节点的路径
         */
        $nPath = [];
        while(true){
            // echo "p->val = ", $p->val, PHP_EOL;

            /**
             * @since 2023-12-25 保存路径
             */
            $pPath[($p->val)] = ($p->val);

            /**
             * @since 2023-12-25 是否存在父节点
             */
            if(isset($this->parentArray[$p->val])){
                $p = $this->parentArray[$p->val];
            } else {
                break;
            }
        }
        // echo "pPath = ", implode(",", $pPath), PHP_EOL;

        /**
         * @since 2023-12-25 $q到根节点的路径
         */
        while(true){
            /**
             * @since 2023-12-25 $q是否在$p到根节点的路径上
             */
            // echo "q->val = ", $q->val, PHP_EOL;
            if(isset($pPath[$q->val])){
                /**
                 * @return
                 */
                return $q;
            } else {}

            /**
             * @since 2023-12-25 向上找父节点
             */
            if(isset($this->parentArray[$q->val])){
                $q = $this->parentArray[$q->val];
            } else {
                /**
                 * @since 2023-12-25 没有父节点
                 */
                return null;
            }
        }
    }

    /**
     * @title 建立子节点和父节点的关系
     * @author start2004
     * @since 2023-12-25 Created
     *
     * @param TreeNode $root
     * @param TreeNode $parentValue
     */
    function ParentRelation($root, $parentTree){
        if($root !== null){
            $this->parentArray[$root->val] = $parentTree;
            $this->ParentRelation($root->left, $root);
            $this->ParentRelation($root->right, $root);
        } else {}
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.25_3.php
 */
$datas = [
    [[3,5,1,6,2,0,8,null,null,7,4], 5, 1], // 3
    // [[3,5,1,6,2,0,8,null,null,7,4], 5, 4], // 5
    // [[1,2], 1, 2], // 1
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    $result = $obj->lowestCommonAncestor($xhprof->tree($data[0]), $xhprof->tree([$data[1]]), $xhprof->tree([$data[2]]));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
